import { useEffect, useState } from "react";
import { useRouteParams } from "./useRouteParams";
export const useHandRouterChange = (
  callback: Function,
  params: string,
  obj?: any,
) => {
  const [data, setData] = useState();
  const [loading, setLoading] = useState(false);
  const routeParams = useRouteParams(params);
  useEffect(() => {
    setLoading(true);
    if (routeParams) {
      callback({ keyWord: routeParams, ...obj })
        .then((res: any) => {
          console.log("回调函数执行完毕1", res);
          setData(res.data);
        })
        .finally(() => {
          setLoading(false);
        });
    } else {
      callback({ ...obj })
        .then((res: any) => {
          console.log("回调函数执行完毕2", res);
          setData(res.data);
        })
        .finally(() => {
          setLoading(false);
        });
    }
  }, [routeParams, callback]);
  return { loading, data };
};
